package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import com.prs.crm.domain.oa.NoticeCollectType;
import com.prs.crm.service.oa.NoticeCollectTypeService;

public class NoticeCollectTypeAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private NoticeCollectType noticeCollectType;
	private NoticeCollectTypeService noticeCollectTypeService;
	private Integer[] typeIds;
	
	@Action("listNoticeCollectType")
	public String listNoticeCollectType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NoticeCollectType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.noticeCollectTypeService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editNoticeCollectType")
	public String editNoticeCollectType() {
		if(this.typeIds!=null){
			this.noticeCollectType=this.noticeCollectTypeService.get(typeIds[0]);
		}
		return SUCCESS;
	}
	@Action(value = "saveNoticeCollectType", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeCollectType"),
			@Result(name = "error", type = "chain", location = "editNoticeCollectType") })
	public String saveNoticeCollectType() {
		if(this.noticeCollectType.getId()==null){
			this.noticeCollectTypeService.save(noticeCollectType);
		}else{
			this.noticeCollectTypeService.submit(noticeCollectType);
		}
		return SUCCESS;
	}
	@Action(value = "removeNoticeCollectType", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeCollectType"),
			@Result(name = "error", type = "chain", location = "listNoticeCollectType") })
	public String removeNoticeCollectType() {
		for(int i=0;i<typeIds.length;i++){
			this.noticeCollectType=this.noticeCollectTypeService.get(typeIds[i]);
			if(noticeCollectType.getCollects().size()!=0){
				this.addActionError("类别已关联收藏，不能删除");
				return ERROR;
			}
		}
		this.noticeCollectTypeService.batchRemove(typeIds);
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public NoticeCollectType getNoticeCollectType() {
		return noticeCollectType;
	}

	public void setNoticeCollectType(NoticeCollectType noticeCollectType) {
		this.noticeCollectType = noticeCollectType;
	}

	public NoticeCollectTypeService getNoticeCollectTypeService() {
		return noticeCollectTypeService;
	}

	public void setNoticeCollectTypeService(
			NoticeCollectTypeService noticeCollectTypeService) {
		this.noticeCollectTypeService = noticeCollectTypeService;
	}

	public Integer[] getTypeIds() {
		return typeIds;
	}

	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}
	
}
